#!/bin/bash

###
### This file is part of icecode-graphics.
###
###    icecode-graphics is free software: you can redistribute it and/or modify
###    it under the terms of the GNU General Public License as published by
###    the Free Software Foundation, either version 3 of the License, or
###    (at your option) any later version.
###
###    icecode-graphics is distributed in the hope that it will be useful,
###    but WITHOUT ANY WARRANTY; without even the implied warranty of
###    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
###    GNU General Public License for more details.
###
###    You should have received a copy of the GNU General Public License
###    along with icecode-graphics.  If not, see <http://www.gnu.org/licenses/>.
###

###
### Common routine library
###

# findProgram (program) : bool => string
# Print the program path (if found) to STDOUT, and return a boolean.
# True if found, false if not.
function findProgram ()
{
    prog=$1
    oifs=$IFS
    IFS=':'
    
    for pth in $PATH; do
        path="$pth/$prog"

        if [[ -x "$path" ]]; then
            echo -n "$path"
            true
            return
        fi
    done

    false
}

# isOlderThan (file1, file2) : boolean
# Check if file1 is older than file2
function isOlderThan ()
{
    if [[ -f $1 ]]; then
        epoch1=$(stat -c "%Y" $1)
    else
        epoch1=0
    fi

    if [[ -f $2 ]]; then
        epoch2=$(stat -c "%Y" $2)
    else
        epoch2=0
    fi

    return $(( $epoch1 >= $epoch2 ))
}
